package com.jk.kdd.system.service;

import com.github.pagehelper.PageHelper;
import com.jk.kdd.system.dao.SystemApplyInfoMapper;
import com.jk.kdd.system.model.SystemApplyInfo;
import com.jk.kdd.system.vo.SystemApplyInfoVO;
import com.jk.util.BaseDataUtil;
import org.smart4j.framework.helper.BeanHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.BeanUtil;
import util.PagedResult;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by DELL on 2016/10/9.
 */
@Service("systemApplyInfoService")
public class SystemApplyInfoServiceImpl implements SystemApplyInfoService {

    @Resource
    private SystemApplyInfoMapper systemApplyInfoMapper;


    /**
     * 查询所有
     */
    public PagedResult<SystemApplyInfoVO> getSystemApplyInfoList(String pageNum,String pageSize) {

        Map<String, Object> paramMap = new HashMap<>();
        PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        List<SystemApplyInfo> applyInfoList = systemApplyInfoMapper.selectByCondition(paramMap);
        //根据Class封装成PagedResult
        PagedResult<SystemApplyInfoVO> pagedResult = BeanUtil.toPagedResult(applyInfoList, SystemApplyInfoVO.class);
        return pagedResult;
    }


    /**
     * 根据主键查找实体
     * @param systemApplyInfo
     * @return
     */
    @Override
    public SystemApplyInfoVO getSystemApplyInfoByPk(SystemApplyInfo systemApplyInfo) {
        systemApplyInfo = systemApplyInfoMapper.selectByPrimaryKey(systemApplyInfo.getId());

        SystemApplyInfoVO systemApplyInfoVO = new SystemApplyInfoVO();
        BeanHelper.copyBean(systemApplyInfo,systemApplyInfoVO);
        return systemApplyInfoVO;
    }

    /**
     * 创建实体
     * @param systemApplyInfo
     * @param operatorID
     * @return
     */
    @Override
    @Transactional
    public boolean createSystemApplyInfo(SystemApplyInfo systemApplyInfo, String operatorID) {

        BaseDataUtil.setBaseDataForEntity(systemApplyInfo,"1",operatorID);
        systemApplyInfoMapper.insertSelective(systemApplyInfo);

        return true;
    }

    /**
     * 修改实体
     * @param systemApplyInfo
     * @param operatorID
     * @return
     */
    @Override
    @Transactional
    public boolean modifySystemApplyInfo(SystemApplyInfo systemApplyInfo, String operatorID) {
        // 设置基础数据
        SystemApplyInfo temp = systemApplyInfoMapper.selectByPrimaryKey(systemApplyInfo.getId());
        systemApplyInfo.setUpdateCount(temp.getUpdateCount());
        // 更新
        BaseDataUtil.setBaseDataForEntity(systemApplyInfo,"2",operatorID);
        systemApplyInfoMapper.updateByPrimaryKeySelective(systemApplyInfo);

        return true;
    }

    /**
     * 删除实体
     * @param systemApplyInfo
     * @param operatorID
     * @return
     */
    @Override
    @Transactional
    public boolean removeSystemApplyInfo(SystemApplyInfo systemApplyInfo, String operatorID) {
        // 设置基础数据
        SystemApplyInfo temp = systemApplyInfoMapper.selectByPrimaryKey(systemApplyInfo.getId());
        systemApplyInfo.setUpdateCount(temp.getUpdateCount());
        // 删除
        BaseDataUtil.setBaseDataForEntity(systemApplyInfo,"3",operatorID);
        systemApplyInfoMapper.updateByPrimaryKeySelective(systemApplyInfo);

        return true;
    }
}
